package BlockingQueueInstance;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class UsingBlockingQueue {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> Queue = new ArrayBlockingQueue<>(1000);
        Thread t1 = new Thread(() -> {
            int n = 0;
            while(true) {
                System.out.println("create "+n);
                try {
                    Queue.put(n);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                n++;
            }
        });
        Thread t2 = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                try {
                    System.out.println("consumed "+Queue.take());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        t1.start();
        t2.start();
    }
}
